#include "mydialog.h"

MyDialog::MyDialog(QWidget* parent):
    QDialog(parent),
    socket(0),
    timer(0),
    connected(false)
{
    setWindowIcon(QIcon(":/icon/WebAlarmClient.ico"));
    mainLayout = new QGridLayout(this);
    ipChangePushButton = new QPushButton("Change IP");
    ipChangePushButton->setEnabled(false);
    ipLineEdit = new QLineEdit;
    QRegExp ipRegExp("^((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))\\.((\\d{1,2})|([01]\\d{2,2})|(2[0-5]{2,2}))");
    QRegExpValidator* ipValidator = new QRegExpValidator(ipRegExp,ipLineEdit);
    ipLineEdit->setValidator(ipValidator);
    maskChangePushButton = new QPushButton("Change Subnet Mask");
    maskChangePushButton->setEnabled(false);
    maskLineEdit = new QLineEdit;
    statusLabel = new QLabel;
    connectionLabel = new QLabel;
    connectLineEdit = new QLineEdit("10.3.7.171");
    connectButton = new QPushButton("Connect");
    socket = new QTcpSocket;
    timer = new QTimer;
    if(QSystemTrayIcon::isSystemTrayAvailable())
    {
        tray = new QSystemTrayIcon(QIcon(":/icon/WebAlarmClient.ico"),this);
        QMenu* trayContextMenu = new QMenu("Web Alarm Client",this);
        QAction* exitAction = new QAction("Exit",trayContextMenu);
        QObject::connect(exitAction,SIGNAL(triggered()),qApp,SLOT(quit()));
        QObject::connect(tray,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(windowShow(QSystemTrayIcon::ActivationReason)));
        trayContextMenu->addAction(exitAction);
        tray->setContextMenu(trayContextMenu);
        tray->show();
    }
    statusLabel->setText("Response here");
    connectionLabel->setText("Disconnected");
    mainLayout->addWidget(ipLineEdit,1,1);
    mainLayout->addWidget(ipChangePushButton,1,2);
    mainLayout->addWidget(maskLineEdit,2,1);
    mainLayout->addWidget(maskChangePushButton,2,2);
    mainLayout->addWidget(statusLabel,3,1,1,2,Qt::AlignCenter);
    mainLayout->addWidget(connectionLabel,4,1,1,2,Qt::AlignCenter);
    mainLayout->addWidget(connectLineEdit,5,1);
    mainLayout->addWidget(connectButton,5,2);
    QObject::connect(connectButton,SIGNAL(clicked()),this,SLOT(hostConnect()));
    QObject::connect(ipChangePushButton,SIGNAL(clicked()),this,SLOT(ipChange()));
    QObject::connect(maskChangePushButton,SIGNAL(clicked()),this,SLOT(maskChange()));
    QObject::connect(timer,SIGNAL(timeout()),this,SLOT(update()));
    setLayout(mainLayout);
    setMaximumSize(sizeHint());
    setMinimumSize(sizeHint());
}
